'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    About:
    Collection of methods for working with color.
'''

'''
============================================================
--->>   IMPORT MODULES
============================================================
'''
import maya.OpenMaya as OpenMaya

'''
============================================================
--->>   UTILITY METHODS
============================================================
'''
def convertRGB( inType=1, inRGB=[] ):
    '''
    Converts and RGB value between 0-1 and 0-255.
    
    @param inType: Int. 1 or 2.
        1, converts 0-1 values to 0-255.
        2, converts 0-255 values to 0-1.
    @param inRGB: List of Floats. RGB value.
    @param return: List of Floats. Converted RGB value. 
    '''
    result = []
    if inType == 1:
        for channel in inRGB:
            result.append( channel * 255 )
    elif inType == 2:
        for channel in inRGB:
            result.append( channel / 255 )
    return result    
    
def getUserDefinedColors( inType=1 ):
    '''
    Gets the current user defined colors.
    
    @inType: Int. 1 is RGB. 2 is HSV.
    @return: List of Lists. User defined colors 1-8.
    '''
    userColors = []
    for color in xrange( 1, 9 ):
        userString = 'userDefined{0}'.format( color )
        if inType == 1:
            melCommand = 'displayRGBColor \"{0}\" -query'.format( userString )
        elif inType == 2:
            melCommand = 'displayRGBColor \"{0}\" -query -hueSaturationValue'.format( userString )
        result = OpenMaya.MDoubleArray()
        OpenMaya.MGlobal.executeCommand( melCommand, result ) 
        userColors.append( result )
    return userColors

def setUserDefinedColor( inColorID, inColor ):
    '''
    Set user defined colors. Assumes RGB.
    
    @inColorID: Int. 1-9. ID number of the userDefined color.
    @inColor: List of Floats. RGB value.
    '''    
    userString = 'userDefined{0}'.format( inColorID )
    melCommand = 'displayRGBColor \"{0}\" {1} {2} {3}'.format( userString,
                                                               inColor[0],
                                                               inColor[1],
                                                               inColor[2] )
    OpenMaya.MGlobal.executeCommand( melCommand )

def setObjectDormantColor( inObject=None, inColorID=1 ):
    '''
    Sets the object's dormant wireframe color to the desired userDefined color.
    
    @inObject: String. Object name.
    @inColorID: Int. 1-9. userDefined color to use.
    '''
    melCommand = 'color -userDefined {0} \"{1}\"'.format( inColorID, inObject )
    OpenMaya.MGlobal.executeCommand( melCommand )